import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:xinxuan/common/dialogs/del_dialog.dart';
import 'package:xinxuan/common/dialogs/message_dialog.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:xinxuan/vo/common_vo/common_pagination_vo.dart';
import 'package:xinxuan/vo/msg_vo.dart';
import 'package:xinxuan/vo/store_order_list_vo.dart';

class StoreOrderPageController extends GetxController {

  final CommonPageController commonPageController = Get.find();
  
  /// tab 下标
  var tabIndex = 0.obs;

  var data = StoreOrderListVo(data: [], meta: StoreOrderMeta(aggregate: [], pagination: CommonPagination(total: 0, currentPage: 1))).obs;

  List<Aggregate> initStatusArr = [
    Aggregate(status: -1, pickType: 0, label: '全部', total: 0),
    Aggregate(status: 1, pickType: 0, label: '待付款', total: 0),
    Aggregate(status: 2, pickType: 1, label: '待提货', total: 0),
    Aggregate(status: 2, pickType: 2, label: '待发货', total: 0),
    Aggregate(status: 5, pickType: 2, label: '待收货', total: 0),
    Aggregate(status: 8, pickType: 1, label: '已自提', total: 0),
    Aggregate(status: 6, pickType: 1, label: '已转提', total: 0),
    Aggregate(status: 8, pickType: 2, label: '已完成', total: 0),
    Aggregate(status: 7, pickType: 0, label: '已取消', total: 0),
    Aggregate(status: 10, pickType: 1, label: '已核销', total: 0),
];

  @override
  void onInit() {
    Map<String, dynamic> argumentsMap = Get.arguments;
    tabIndex.value = argumentsMap['tabIndex'] ?? 0;
    if (data.value.meta!.aggregate!.length <= 0) {
      data.value.meta!.aggregate = initStatusArr;
    }
    super.onInit();
  }

  @override
  void onReady() {
    // checkPickType();
    getStoreOrderList();
    super.onReady();
  }

  /// 获取订单列表
  getStoreOrderList() async {
    data.value.meta!.pagination!.currentPage = 1;
    StoreOrderListVo tempVo = await HttpUtilService.getStoreOrderList(status: data.value.meta!.aggregate![tabIndex.value].status!, page: data.value.meta!.pagination!.currentPage!, pickType: data.value.meta!.aggregate![tabIndex.value].pickType!, loading: true);
    data.value = tempVo;
    update();
  }

  /// 加载更多订单列表
  onloadStoreOrderList() async {
    if (data.value.meta!.pagination!.total! <= data.value.data!.length) {
      return;
    }
    data.value.meta!.pagination!.currentPage = data.value.meta!.pagination!.currentPage! + 1;
    StoreOrderListVo tempVo = await HttpUtilService.getStoreOrderList(status: data.value.meta!.aggregate![tabIndex.value].status!, page: data.value.meta!.pagination!.currentPage!, pickType: data.value.meta!.aggregate![tabIndex.value].pickType!, loading: false);
    data.value.data!.addAll(tempVo.data!);
    update();
  }

  /// 取消订单
  Future<bool> storeOrderCancel(int orderId) async {
    var res = await HttpUtilService.storeOrderCancel(orderId: orderId);
    if (res) {
      getStoreOrderList();
    }
    return res;
  }

  /// 确认收货
  Future<bool> storeOrderReceive(int orderId) async {
    var res = await HttpUtilService.storeOrderReceive(orderId: orderId);
    if (res) {
      getStoreOrderList();
    }
    return res;
  }

  /// 转提货
  conversionPickingFun({ required List<Map<String, dynamic>> products, 
                         required int orderId, 
                         required int status,
                         required String statusStr,
                         bool isDetail = false}) {
    Get.dialog(DelDialog(
      title: '转提货',
      message: '转提货后，商品由接受人进行提货，本人不可对订单进行操作，确认吗？', 
      onTap: () {
        if (isDetail) {
          Get.offAndToNamed(
            '${AppRouters.COMMON_PAGE}${AppRouters.STORE_ORDER_PAGE}${AppRouters.CONVERSION_PICKING_PAGE}',
            arguments: {
              'orderId': orderId,
              'products': products,
              'status': status,
              'statusStr': statusStr,
            }
          );
        } else {
          Get.toNamed(
          '${AppRouters.COMMON_PAGE}${AppRouters.STORE_ORDER_PAGE}${AppRouters.CONVERSION_PICKING_PAGE}',
            arguments: {
              'orderId': orderId,
              'products': products,
              'status': status,
              'statusStr': statusStr,
            }
          );
        }
      }
    ));
  }
  /// 提货码
  pickUpCodeFun({ required List<Map<String, dynamic>> products, 
                  required int orderId, 
                  required int status,
                  required String statusStr,
                  bool isDetail = false}) {
    Get.dialog(DelDialog(
      title: '获取提货券码',
      message: '获取提货券码后，不可以再转给别人提货了，确认要生成提货券码吗？', 
      onTap: () {
        if (isDetail) {
          Get.offAndToNamed(
            '${AppRouters.COMMON_PAGE}${AppRouters.STORE_ORDER_PAGE}${AppRouters.PICKUP_CODE_PAGE}',
            arguments: {
              'orderId': orderId,
              'products': products,
              'status': status,
              'statusStr': statusStr,
            }
          );
        } else {
          Get.toNamed(
            '${AppRouters.COMMON_PAGE}${AppRouters.STORE_ORDER_PAGE}${AppRouters.PICKUP_CODE_PAGE}',
            arguments: {
              'orderId': orderId,
              'products': products,
              'status': status,
              'statusStr': statusStr,
            }
          );
        }
      }
    ));
  }

  /// 核销
  Future<bool> checkoff(int orderId) async {
    MsgVo? msgVo = await HttpUtilService.checkoff(id: orderId);
    if (msgVo != null) {
      Get.dialog(MessageDialog(
        message: msgVo.data!.msg ?? '提示异常', 
        title: '提示信息',
        messageColor: textSubtitleColor,
      ));
      return false;
    } else {
      getStoreOrderList();
      return true;
    }
  }

  /// 一键核销
  orderCheckoff() {
    Get.dialog(DelDialog(message: '确认核销吗？', onTap: () async {
      bool res = await HttpUtilService.orderCheckoff();
      if (res) {
        EasyLoading.showToast('核销成功');
        getStoreOrderList();
      }
    }));
  }

}